package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.create.SpEmergencyReportCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.SpEmergencyReportQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpEmergencyReportResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpEmergencyReportUpdateDTO;
import com.engine4cloud.tdf.supervision.service.SpEmergencyReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 审批-突发事件上报-web
 *
 * @author wenjie
 * @date 2024-04-11 10:23:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/emergency/report")
@Tag(description = "EmergencyReportWeb", name = "审批-突发事件上报管理-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SpEmergencyReportWebController {

    private final SpEmergencyReportService emergencyReportService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  突发事件上报
     * @return R<PageInfo < SpEmergencyReportResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<SpEmergencyReportResultDTO>> pageList(@ParameterObject Params page, @Valid @ParameterObject SpEmergencyReportQueryDTO dto) {
        return R.ok(emergencyReportService.pageList(page, dto));
    }


    /**
     * 通过id查询突发事件上报
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<SpEmergencyReportResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(emergencyReportService.getById(id));
    }

    /**
     * 新增突发事件上报
     *
     * @param dto 突发事件上报
     * @return R
     */
    @Operation(summary = "新增突发事件上报", description = "新增突发事件上报")
    @SysLog("新增突发事件上报")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('emergency_add')")
    public R<Boolean> create(@Valid @RequestBody SpEmergencyReportCreateDTO dto) {
        return R.ok(emergencyReportService.create(dto));
    }

    /**
     * 修改突发事件上报
     *
     * @param dto 突发事件上报
     * @return R
     */
    @Operation(summary = "修改突发事件上报", description = "修改突发事件上报")
    @SysLog("修改突发事件上报")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('emergency_edit')")
    public R<Boolean> updateById(@Valid @RequestBody SpEmergencyReportUpdateDTO dto) {
        return R.ok(emergencyReportService.update(dto));
    }

    /**
     * 通过id删除突发事件上报
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除突发事件上报", description = "通过id删除突发事件上报")
    @SysLog("通过id删除突发事件上报")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('emergency_del')")
    public R<Boolean> removeById(@RequestBody String[] ids) {
        return R.ok(emergencyReportService.removeBatchByIds(CollUtil.toList(ids)));
    }
}